<?php
class MenuItem extends MenuItemPeer
{
	const STATUS_ENABLED = 0;
    const STATUS_DISABLED = 1;

	var $className = __CLASS__;
    var $children;

    public function init()
	{
        $this->status = self::STATUS_ENABLED;
	}
	
	/**
	 * Returns the static model of the specified AR class.
	 * @return MenuItem the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);

		$criteria->compare('menu_id',$this->menu_id,true);

		$criteria->compare('parent_id',$this->parent_id,true);

		$criteria->compare('name',$this->name,true);

		$criteria->compare('link',$this->link,true);

		$criteria->compare('description',$this->description,true);

		$criteria->compare('sorder',$this->sorder);

		$criteria->compare('status',$this->status);

		$criteria->compare('options',$this->options,true);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}

	
	
    public function rebuildSOrder($menuId = 0) {
        $criteria=new CDbCriteria;
        $criteria->select = "id, sorder, parent_id";
        $criteria->order = "menu_id, parent_id, sorder,id";
        if($menuId) $criteria->compare('menu_id', $menuId);

        $data = $this->model()->findAll($criteria);
        $row_numbers = count($data);
        $sorder = 0;
        for($i=0; $i<$row_numbers-1; $i++) {
            $sorder++;
            //Save order
            self::model()->updateByPk($data[$i]->id, array('sorder'=>$sorder));

            //Reset order for new node
            if($data[$i]->parent_id!=$data[$i+1]->parent_id)
                $sorder = 0;
        }
        $sorder++;
        self::model()->updateByPk($data[$i]->id, array('sorder'=>$sorder));
    }
    
    


    public static function dumpAllMenuItemToArray($menuId = 0) {
        
    }
    /*
     * Using for sub menu top or menu footer
     * If using for the last menu, add $type = true else $type = false
     */
    public function subMenuItem()
    {
        $criteria = new CDbCriteria();
        $criteria->select = 'id, name, link,parent_id';
        $criteria->addCondition('parent_id ='.$this->id);
        $criteria->addCondition("status= " .MenuItem::STATUS_ENABLED);
        $criteria->order = "sorder";
        $menuItems = MenuItem::model()->findAll($criteria);
        if($menuItems){
            echo '<li class="readmore"><a href="'.$this->link.'" class="icon-readmore"><span>'.$this->name.'</span></a>';
                echo '<ul class="sub">';
                foreach($menuItems as $menuItem)
                    echo '<li><a href="'.$menuItem->link.'">'.$menuItem->name.'</a></li>';
                echo  '</ul>';
            echo '</li>';
        }
    }
    
    public function deleteCache(){
        $cacheName = '_MenuItems_'.$this->menu_id;
        CoreCache::deleteCache($cacheName);
        return;
    }
}